<?php session_start(); ?>
<?php include_once('misc/class_config.php'); ?>
<?php
	include_once('access_control/page_access.php');

	setPreviousPage();
	declinePageIfNotLoggedIn();
	declinePageIfNotAdmin();
	
	$currentUser = new user();
	$currentUser->isLoggedIn();
	$currentUser->isTimedOut();
?>



<?php
	$productExist = false;
	$productCode = 0;
	$existingProduct = new product();
	$productValidationError = new validation();
	
	if (!isset($_POST['btnUpdateProduct']))
	{	
		if(!isset($_REQUEST['code']))
		{
			header("location: ".getPreviousPage());
		}
		else
		{
			$existingProduct->code = $_REQUEST['code'];
			$productCode = $_REQUEST['code'];
		}
	}
	else
	{
		$existingProduct->code = $_POST['product_code'];
		$productCode = $_POST['product_code'];
	}
	if(!$existingProduct->retrieveProduct())
	{
		$productExist = false;
	}
	else
	{
		$productExist = true;
		
		include_once('model/category.php');
		
		$productMessage = '';
		$productNameErrorMessage = '';
		$categoryErrorMessage = '';
		$unitPriceErrorMessage = '';
		$descriptionErrorMessage = '';
		$quantityErrorMessage = '';
		
		if(isset($_POST['btnUpdateProduct']))
		{
			$productNameErrorMessage = $productValidationError->isEmpty(trim($_POST['name']),'<label class="required">First name is required.</label>');
			$categoryErrorMessage = $productValidationError->isListItemSelected(trim($_POST['category']),'<label class="required">Select a category.</label>');
			$unitPriceErrorMessage = $productValidationError->isEmpty(trim($_POST['price']),'<label class="required">Unit Price is required.</label>');
			$descriptionErrorMessage = $productValidationError->isEmpty(trim($_POST['description']),'<label class="required">Description is required.</label>');
			$quantityErrorMessage = $productValidationError->isEmpty(trim($_POST['quantity']),'<label class="required">Quantity is required.</label>');
			if(empty($quantityErrorMessage))
			{
				$quantityErrorMessage = $productValidationError->isNumeric(trim($_POST['quantity']),'<label class="required">Quantity is required.</label>');
			}
			$existingProduct->setProduct($productCode,$_POST['name'],$_POST['price'],$_POST['description'],$_POST['quantity'],$_POST['category']);		
		}
	}
?>

<?php
	if(isset($_POST['btnUpdateProduct']))
	{
		$formError = $productValidationError->status;
		
		if($formError==0)
		{
			try
			{
				if($existingProduct->updateProduct())
				{
					$productMessage = '<span class="success_msg_span">Product updated successfully.</span>';
				}
				else
				{
					$productMessage = '<span class="failed_msg_span">An error occured. Product was not added.</span>';
				}
			}
			catch(Exception $ex)
			{
				$productMessage = '<span class="failed_msg_span">'.$ex->getMessage().'</span>';
			}
		}
	}
?>


<?php $pageTitle = "Edit Product | Everything"; ?>
<?php include_once('layout/page_start.php'); ?>
<?php include_once('layout/page_header.php'); ?>
<?php include_once('layout/menu_bar.php'); ?>

<div id="main_content_div">
<?php 
	if(!$productExist)
	{
		$productMessage = '<span class="failed_msg_span">Product could not be found.</span>';
		echo '<div class="page_action_msg_div"><p>'.$productMessage.'</p></div>';
	}
	else
	{
		echo '<div class="page_action_msg_div"><p>'.$productMessage.'</p></div>';
		include_once('layout/editproductform.php'); 
	}
?>
</div>
<?php include('layout/page_footer.php'); ?>
<?php include('layout/page_end.php'); ?>
<?php setCurrentPage();?>